package com.example.wangshuo.recyclerviewtest_205ws;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;//RecyclerView布局管理
    private  MyAdapter myAdapter;//RecyclerView适配器
    private  List<ArticleBean> list=null;//保存数据

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRecyclerView();
    }
    // 初始化RecyclerView
    private void initRecyclerView() {
        recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
        //创建默认线性布局
        layoutManager=new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //固定每个item高度，提高性能
        recyclerView.setHasFixedSize(true);
        //获取数据
        list=getData();
        //实例化Adapter
        myAdapter=new MyAdapter(this);
        myAdapter.setList(list);//传进数据
        recyclerView.setAdapter(myAdapter);//绑定

    }
    //获取数据源并返回List
    private List<ArticleBean> getData(){
        List<ArticleBean> list=new ArrayList<ArticleBean>();
        ArticleBean articleBean;
        //第一组模拟数据
        articleBean=new ArticleBean();
        articleBean.setArticleId(1);
        articleBean.setTitle("标题一");
        articleBean.setDescription("描述一");
        articleBean.setTime("2017-4-13");
        articleBean.setImgid(R.drawable.f1);
        list.add(articleBean);

        //第二组模拟数据
        articleBean=new ArticleBean();
        articleBean.setArticleId(2);
        articleBean.setTitle("标题2");
        articleBean.setDescription("描述2");
        articleBean.setTime("2017-4-14");
        articleBean.setImgid(R.drawable.f2);
        list.add(articleBean);

        //第三组模拟数据
        articleBean=new ArticleBean();
        articleBean.setArticleId(3);
        articleBean.setTitle("标题三");
        articleBean.setDescription("描述三");
        articleBean.setTime("2017-4-15");
        articleBean.setImgid(R.drawable.f3);
        list.add(articleBean);

        //第四组模拟数据
        articleBean=new ArticleBean();
        articleBean.setArticleId(4);
        articleBean.setTitle("标题四");
        articleBean.setDescription("描述四");
        articleBean.setTime("2017-4-16");
        articleBean.setImgid(R.drawable.f4);
        list.add(articleBean);

        //第五组模拟数据
        articleBean=new ArticleBean();
        articleBean.setArticleId(5);
        articleBean.setTitle("标题五");
        articleBean.setDescription("描述五");
        articleBean.setTime("2017-4-17");
        articleBean.setImgid(R.drawable.f5);
        list.add(articleBean);

        //第六组模拟数据
        articleBean=new ArticleBean();
        articleBean.setArticleId(6);
        articleBean.setTitle("标题六");
        articleBean.setDescription("描述六");
        articleBean.setTime("2017-4-18");
        articleBean.setImgid(R.drawable.f6);
        list.add(articleBean);
        return list;

    }
}
